class ToolInfoView extends egret.Sprite{
    private _bgMask:egret.Shape; // 弹出框的遮罩
    private _contentView:egret.DisplayObjectContainer; // 弹出框的内容容器
    private _contentviewbg:TbitMap;// 背景
    private _contentviewtitle:BaseTextField; // 标题
    private _close:TbitMap; // 关闭按钮

    private contentHeight:number;
    private _toolImage:eui.Image;//道具图片
    private _toolImage2:eui.Image;//道具图片
    private _arrow:TbitMap;
    private _arrowtest:BaseTextField;
    private _toolName:BaseTextField; //道具名称
    private _toolContent:BaseTextField; //道具说明
    private maskAlpha:number;

    public constructor(){
        super();

        this.maskAlpha = 0;

        this.width = GameData.stageW;
        this.height = GameData.stageH;
    }



    private data = {};
    public open(data:any){
        this.data = data;
        if(!this.contains(this.bgMask))
        {
            this.addChild(this.bgMask);
        }

        // 内容容器
        if(!this.contains(this.contentView))
        {
            this.addChild(this.contentView);
        }

        if(!this.contentView.contains(this.contentviewbg))
        {
            this.contentView.addChild(this.contentviewbg);
            this.contentView.setChildIndex(this.contentviewbg, 0);
        }

        // 关闭按钮
        if(!this.contentView.contains(this.closeBtn))
        {
            this.contentView.addChild(this.closeBtn);
        }
        if(!this.contentView.contains(this.contentviewtitle))
        {
            this.contentView.addChild(this.contentviewtitle);
        }
        this._contentviewtitle.text = data.type == 1?'道具信息':data.name;


        if(data.type == 1)
        {
            if(!this.contentView.contains(this.toolImage)){
                this.contentView.addChild(this.toolImage);
            }
            this._toolImage.source = data.img;
            this._toolImage.x = (this.contentviewbg.width - this.toolImage.width) / 2;

            if(this.contentView.contains(this.toolImage2)){
                this.contentView.removeChild(this.toolImage2);
            }

            if(this.contentView.contains(this.arrow)){
                this.contentView.removeChild(this.arrow);
            }

            if(this.contentView.contains(this.arrowtest)){
                this.contentView.removeChild(this.arrowtest);
            }

            if(!this.contentView.contains(this.toolName))
            {
                this.contentView.addChild(this.toolName);
            }
            this._toolName.text = data.name;
            this._toolName.x = (this.contentviewbg.width - this.toolName.width) / 2;
        }
        else
        {
            if(!this.contentView.contains(this.toolImage)){
                this.contentView.addChild(this.toolImage);
            }
            this._toolImage.source = data.img;
            this._toolImage.x = 80;

            if(!this.contentView.contains(this.toolImage2)){
                this.contentView.addChild(this.toolImage2);
            }
            this._toolImage2.source = data.parent_img;
            this._toolImage2.x = this.contentView.width - 215;

            if(!this.contentView.contains(this.arrow)){
                this.contentView.addChild(this.arrow);
            }

            if(!this.contentView.contains(this.arrowtest)){
                this.contentView.addChild(this.arrowtest);
            }

            if(this.contentView.contains(this.toolName))
            {
                this.contentView.removeChild(this.toolName);
            }
        }

        if(!this.contentView.contains(this.toolContent))
        {
            this.contentView.addChild(this.toolContent);
        }
        if(data.type == 1)
        {
            this._toolContent.text = data.desc;
        }
        else
        {
            this._toolContent.text = data.parent_name + '-' + data.parent_desc;
        }

        // 添加动画
        this.addBaseAnimation();
    }


    private addBaseAnimation(): void {
        this.contentView.scaleX = 0.8;
        this.contentView.scaleY = 0.8;
        var tw = egret.Tween.get(this.contentView);
        tw.to({ scaleX: 1, scaleY: 1 }, 500, egret.Ease.backOut);
    }

    public get contentView(): egret.DisplayObjectContainer {
        if(!this._contentView)
        {
            var contentView = new egret.DisplayObjectContainer();
            contentView.width = 690 * 0.88;
            contentView.height = 600 * 0.88;
            contentView.anchorOffsetX = contentView.width / 2;
            contentView.anchorOffsetY = contentView.height / 2;
            contentView.x = this.width / 2.0;
            contentView.y = this.height / 2.0;
            this._contentView = contentView;
        }
        return this._contentView;
    }

    protected get bgMask(): egret.Shape {
        if(!this._bgMask)
        {
            var bgMask = new egret.Shape();
            bgMask.graphics.beginFill(0x000000, this.maskAlpha);
            bgMask.graphics.drawRect(0, 0, this.width, this.height);
            bgMask.graphics.endFill();
            bgMask.touchEnabled = true;
            this._bgMask = bgMask;
        }
        return this._bgMask;
    }

    private get contentviewbg():TbitMap{
        // console.log(this._contentviewbg);
        if(!this._contentviewbg){
            var contentviewbg = utils.createBitmapByName('dj_tk');
            contentviewbg.height = this.contentView.height;
            contentviewbg.width = this.contentView.width;
            this._contentviewbg = contentviewbg;
        }
        return this._contentviewbg;
    }

    public get closeBtn():TbitMap{
        if(!this._close){
            var close = utils.createBitmapByName("sure");
            close.anchorOffsetX = close.width / 2;
            close.x = this.contentView.width/2;
            close.y = this.contentView.height - close.height * 1.25;
            close.touchAnimation = true;
            close.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closeAction,this);
            this._close = close;
        }
        return this._close;
    }

    public get contentviewtitle()
    {
        if(!this._contentviewtitle){
            var title = new BaseTextField();
            title.text = '';
            title.size = 25;
            title.textColor = 0xffffff;
            title.stroke = 2;
            title.strokeColor = 0x663E0D;
            title.width = this.contentView.width;
            title.height = 40;
            title.textAlign = egret.HorizontalAlign.CENTER;
            title.verticalAlign = egret.VerticalAlign.MIDDLE;
            this._contentviewtitle = title;
        }
        return this._contentviewtitle;
    }


    public get toolImage():eui.Image{
        if(!this._toolImage){
            var image = new eui.Image();
            image.width = 135;
            image.height = 135;
            image.y = 75;
            this._toolImage = image;
        }

        return this._toolImage;
    }

    public get toolImage2():eui.Image{
        if(!this._toolImage2){
            var image = new eui.Image();
            image.width = 135;
            image.height = 135;
            image.y= 75;
            this._toolImage2 = image;
        }

        return this._toolImage2;
    }

    public get arrow():TbitMap{
        if(!this._arrow){
            var image = utils.createBitmapByName("dj_arrow");
            image.width = 114;
            image.height = 70;
            image.y = 100;
            image.x = (this.contentView.width - image.width) / 2;
            this._arrow = image;
        }

        return this._arrow;
    }

    public get arrowtest():BaseTextField{
        if(!this._arrowtest){
            var title = new BaseTextField();
            title.text = '1,2,3,4可合成';
            title.size = 20;
            title.textColor = 0xfde157;
            title.stroke = 2;
            title.strokeColor = 0x663E0D;
            title.height = 40;
            title.verticalAlign = egret.VerticalAlign.TOP;
            title.y = 180;
            title.x = (this.contentView.width - title.width) / 2;
            title.bold = true;
            this._arrowtest = title;
        }

        return this._arrowtest;
    }

    private get toolName():BaseTextField{
        if(!this._toolName){
            var title = new BaseTextField();
            title.text = '';
            title.size = 24;
            title.textColor = 0xfde157;
            title.stroke = 2;
            title.strokeColor = 0x663E0D;
            title.height = 40;
            title.verticalAlign = egret.VerticalAlign.TOP;
            title.y = 230;
            title.bold = true;
            this._toolName = title;
        }

        return  this._toolName;
    }

    private get toolContent():BaseTextField{
        if(!this._toolContent){
            var shape = new egret.Shape();
            shape.width = this.contentviewbg.width - 50;
            shape.height = 30 + 30 * Math.ceil(this.data['desc'].length / 10);
            if(shape.height < 90){
                shape.height = 90;
            }
            if(shape.height > 90){
                this.toolImage2.y = this.toolImage.y = this.toolImage.y + 15;
            }
            shape.graphics.beginFill(0x70511A);
            shape.graphics.drawRoundRect(0,0,shape.width,shape.height,30,30);
            shape.graphics.endFill;
            shape.x = 25;
            shape.y = 270;
            this.contentView.addChild(shape);

            var title = new BaseTextField();
            title.text = '';
            title.size = 24;
            title.textColor = 0xfde157;
            title.stroke = 2;
            title.strokeColor = 0x663E0D;
            title.height = shape.height - 20;
            title.width = shape.width - 20;
            title.x = shape.x + 10;
            title.y = shape.y + 10;
            title.lineSpacing = 15;
            title.textAlign = egret.HorizontalAlign.CENTER ;
            title.verticalAlign = egret.VerticalAlign.MIDDLE;
            this._toolContent = title;
        }

        return  this._toolContent;
    }

    private closeAction(event:egret.TouchEvent):void {
       Main.removeTooInfoView();
    }
}